package com.byl.sdk.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 字符串常用工具类
 * @ProjectName: tools
 * @Package: com.hjf.tools
 * @Author: zhangpeiyu
 * @CreateDate: 2018/3/7 10:13
 * @Version: 1.0
 */

public class DateUtils {

    private static SimpleDateFormat second = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
    private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");
    private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");
    private static SimpleDateFormat ymDate = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat ymdDate = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat ymdhmDate = new SimpleDateFormat("yyyyMMddHHmmss");


    /**
     * 比较两个日期型字符串大小
     *
     * @param date1 日期字符串1
     * @param date2 日期字符串2
     * @return 日期1大于日期2 大于返回1 等于返回0 否则返回-1
     */
    public static int compare_date(String date1, String date2) {
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = second.parse(date1);
            dt2 = second.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() < dt2.getTime()) {
            return -1;
        } else if (dt1.getTime() > dt2.getTime()) {
            return 1;
        }
        return 0;
    }

    /**
     * 日期转换成字符串
     *
     * @param date 要转换的日期
     * @return 返回转换后的字符串
     */
    public static String dateToStr(Date date) {
        String str = second.format(date);
        return str;
    }

    /**
     * 字符串转换成日期
     *
     * @param str 要转换的字符串
     * @return 返回转换后的日期
     */
    public static Date strToDate(String str) {
        Date date = null;
        try {
            date = second.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 得到当前时间，以字符串表示
     *
     * @return
     */
    public static String getDate() {
        Date date = new Date();
        return DateUtils.dateToStr(date);
    }

    /**
     * 返回 年号+月号+天+时+分+秒+随机码
     *
     * @return
     */
    public static synchronized String getTime() {
        Calendar calendar = Calendar.getInstance();
        String year = calendar.get(calendar.YEAR) + "";
        String month = (calendar.get(calendar.MONTH) + 1) + "";
        String day = calendar.get(calendar.DAY_OF_MONTH) + "";
        String hour = calendar.get(calendar.HOUR_OF_DAY) + "";
        String minute = calendar.get(calendar.MINUTE) + "";
        String second = calendar.get(calendar.SECOND) + "";
        String milliSecond = calendar.get(calendar.MILLISECOND) + "";
        int r = (int) (Math.random() * 100000);
        String random = String.valueOf(r);
        return year + month + day + hour + minute + second + milliSecond + random + "a";
    }

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }


    /**
     * 格式化excel中的时间
     *
     * @param date
     * @return
     */
    public static String formatDateForExcelDate(Date date) {
        return excelDate.format(date);
    }

    /**
     * 格式 化年月
     * @param
     * @return
     */
    public static String formatDateForYmDate() {
       Date date=Calendar.getInstance().getTime();
        return ymDate.format(date);
    }

    /**
     * 将日期格式化作为文件名
     *
     * @param date
     * @return
     */
    public static String formatDateForFileName(Date date) {
        return fileName.format(date);
    }

    /**
     * 格式化日期(精确到秒)
     *
     * @param date
     * @return
     */
    public static String formatDateSecond(Date date) {
        return second.format(date);
    }

    /**
     * 格式化日期(精确到秒)
     *
     * @param date
     * @return
     */
    public static String tempDateSecond(Date date) {
        return tempTime.format(date);
    }

    public static Date tempDateSecond(String str) {
        try {
            return tempTime.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    /**
     * 格式化日期(精确到天)
     *
     * @param date
     * @return
     */
    public static String formatDateDay(Date date) {
        return day.format(date);
    }

    /**
     * 格式化日期(精确到天)
     *
     * @param date
     * @return
     */
    public static String formatDateDetailDay(Date date) {
        return detailDay.format(date);
    }


    /**
     * 将double类型的数字保留两位小数（四舍五入）
     *
     * @param number
     * @return
     */
    public static String formatNumber(double number) {
        DecimalFormat df = new DecimalFormat();
        df.applyPattern("#0.00");
        return df.format(number);
    }

    /**
     * 将字符串转换成日期
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date formateDate(String date) throws Exception {
        return day.parse(date);
    }

    /**
     * 将字符日期转换成Date
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date parseStringToDate(String date) throws Exception {
        return day.parse(date);
    }

    public static String formatDoubleNumber(double number) {
        DecimalFormat df = new DecimalFormat("#");
        return df.format(number);
    }

    /**
     * 获取系统时间
     * @return
     */
    public static String getSystemTime(){
        Calendar calendar=Calendar.getInstance();
        Date now=calendar.getTime();
        return tempTime.format(now);
    }

    public static String getSysYm(){
        Calendar calendar=Calendar.getInstance();
        Date now=calendar.getTime();
        return ymDate.format(now);
    }

    public static String getSysYmd(){
        Calendar calendar=Calendar.getInstance();
        Date now=calendar.getTime();
        return ymdDate.format(now);
    }

    public static String getSysYmdhm(){
        Calendar calendar=Calendar.getInstance();
        Date now=calendar.getTime();
        return ymdhmDate.format(now);
    }

    /**
     * 获取当前周几
     * @return
     */
    public static String getCurrentDayOfWeek(){
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static void main(String []args){
        System.out.println(getCurrentDayOfWeek());
    }


}


